package com.xx.job.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xx.job.entity.system.SysRole;
import com.xx.job.entity.system.SysRoleDept;
import com.xx.job.mapper.SysRoleDeptMapper;
import com.xx.job.service.system.SysDeptService;
import com.xx.job.service.system.SysRoleDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangcy
 * @since 2021-01-14
 */
@Service
public class SysRoleDeptServiceImpl extends ServiceImpl<SysRoleDeptMapper, SysRoleDept> implements SysRoleDeptService {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysRoleDeptMapper sysRoleDeptMapper;
    @Override
    public Map<String, Object> roleDeptTreeselect(Long roleId) {
        Map<String,Object> data = new HashMap<>();
        List<Long> deptIds = this.selectByRoleId(roleId);
        List<Map<String, Object>> deptTree = sysDeptService.deptTreeselect();
        data.put("checkedKeys",deptIds);
        data.put("depts",deptTree);
        data.put("code",200);
        return data;
    }

    @Override
    public List<Long> selectByRoleId(Long roleId) {
        List<Long> deptIds = new ArrayList<>();
        QueryWrapper<SysRoleDept> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<SysRoleDept> sysRoleDepts = sysRoleDeptMapper.selectList(wrapper);
        sysRoleDepts.forEach(x ->{
            deptIds.add(x.getDeptId());
        });
        return deptIds;
    }

    @Override
    public void add(SysRole sysRole) {
        Long roleId = sysRole.getRoleId();
        List<Long> deptIds = sysRole.getDeptIds();
        deptIds.forEach(x ->{
            SysRoleDept sysRoleDept = new SysRoleDept();
            sysRoleDept.setRoleId(roleId);
            sysRoleDept.setDeptId(x);
            sysRoleDeptMapper.insert(sysRoleDept);
        });
    }


}
